package com.itszt.manager.service.impl;

import com.itszt.common.EasyUITreeBean;
import com.itszt.manager.dao.ITbItemCatDao;
import com.itszt.manager.service.ItemCatService;
import com.itszt.pojo.TbItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private ITbItemCatDao tbItemCatDao;
    @Override
    public List<EasyUITreeBean> showTopNodes() {
        List<TbItemCat> tbItemCats = tbItemCatDao.findByParentId(0L);
        List<EasyUITreeBean> easyUITreeBeans = convertItemCat(tbItemCats);
        return easyUITreeBeans;
    }

    @Override
    public List<EasyUITreeBean> showLeafNodes(String parentID) {
        List<TbItemCat> tbItemCats = tbItemCatDao.findByParentId(Long.parseLong(parentID));
        List<EasyUITreeBean> easyUITreeBeans = convertItemCat(tbItemCats);
        return easyUITreeBeans;
    }

    private List<EasyUITreeBean> convertItemCat(List<TbItemCat> tbItemCats){
        List<EasyUITreeBean> easyUITreeBeans=new ArrayList<>();
        for (TbItemCat tbItemCat : tbItemCats) {
            EasyUITreeBean easyUITreeBean=new EasyUITreeBean();
            easyUITreeBeans.add(easyUITreeBean);
            easyUITreeBean.setId(tbItemCat.getId());
            easyUITreeBean.setText(tbItemCat.getName());
            if (!tbItemCat.getIsParent()) {
                easyUITreeBean.setState(EasyUITreeBean.STATE_OPEN);

            }
        }

        return easyUITreeBeans;
    }
}
